﻿using Future.Common.Models;
using Future.IDAL;
using Future.Model.DtoModel;
using Future.Model.SysModel;
using System.Linq.Expressions;

namespace Future.DAL
{
    /// <summary>
    /// 描述：用户数据访问类
    /// 创建人： yangkai
    /// 创建时间：2022/11/9 13:44:47
    /// </summary>
    public class UserDAL : BaseDAL<UserModel>, IUserDAL
    {
        public PageModel<UserModel> AllocatedUserPageList(UserQueryDto userQueryDto, PageParModel page)
        {
            string sql = @"select Sys_User.*
                            from Sys_User
                            join Sys_UserRole on Sys_User.Id = Sys_UserRole.UserId
                            where Sys_User.Status = 1 and RoleId = @RoleId";
            Dictionary<string, object> parameters = new()
            {
                { "RoleId", userQueryDto.RoleId }
            };
            return PageList(sql, parameters, page);
        }

        public PageModel<UserModel> GetUserPageList(UserQueryDto queryDto, PageParModel page)
        {
            //动态条件动态构建lambda表达式，不建议这么玩太麻烦，建议走SQL的方式
            //SqlSugar本来也有封装的组装动态lambda，但是在这里不使用。不在此类引入SqlSugar
            //想用的可以自己封装，简化组装动态lambda
            Expression<Func<UserModel, bool>> where = x => true;
            Expression whereExpr = where.Body;

            if (!string.IsNullOrEmpty(queryDto.UserName))
            {
                Expression<Func<UserModel, bool>> userNameExpr = x => x.UserName == queryDto.UserName;
                whereExpr = Expression.AndAlso(whereExpr, userNameExpr.Body);
            }

            if (!string.IsNullOrEmpty(queryDto.NickName))
            {
                Expression<Func<UserModel, bool>> nickNameExpr = x => x.NickName.Contains(queryDto.NickName);
                whereExpr = Expression.AndAlso(whereExpr, nickNameExpr.Body);
            }

            if (queryDto.Status.HasValue)
            {
                Expression<Func<UserModel, bool>> statusExpr = x => x.Status == queryDto.Status;
                whereExpr = Expression.AndAlso(whereExpr, statusExpr.Body);
            }

            if (queryDto.StartTime.HasValue)
            {
                Expression<Func<UserModel, bool>> createTimeExpr = x => x.CreateTime > queryDto.StartTime.Value;
                whereExpr = Expression.AndAlso(whereExpr, createTimeExpr.Body);
            }

            if (queryDto.EndTime.HasValue)
            {
                Expression<Func<UserModel, bool>> createTimeExpr = x => x.CreateTime <= queryDto.EndTime.Value;
                whereExpr = Expression.AndAlso(whereExpr, createTimeExpr.Body);
            }

            Expression<Func<UserModel, bool>> lambda = Expression.Lambda<Func<UserModel, bool>>(whereExpr, where.Parameters);
            return PageList(lambda, page);
        }

        public PageModel<UserModel> UnallocatedPageList(UserQueryDto userQueryDto, PageParModel page)
        {
            string sql = @"SELECT  Id, UserName, NickName, AvatarFileId, Password, Status, CreateBy, CreateTime, UpdateBy, UpdateTime, Remark
                FROM      Sys_User
                WHERE  Sys_User.Status = 1 and (NOT EXISTS
                       (SELECT  1 AS Expr1
                        FROM       Sys_UserRole
                        WHERE    (Sys_User.Id = UserId) AND (RoleId = @RoleId)))";
            Dictionary<string, object> parameters = new()
            {
                { "RoleId", userQueryDto.RoleId }
            };
            return PageList(sql, parameters, page);
        }
    }
}
